package com.lastproject.web.bean;

import javax.faces.model.DataModel;
import javax.faces.model.DataModelListener;

public class SurveyStatsQSortFilterModel extends
		DataModel<SurveyStatsBean> {
	private DataModel<SurveyStatsBean> model;
	private Row[] rows;

//	private static Comparator<Row> byTitle = new Comparator<Row>() {
//		public int compare(Row r1, Row r2) {
//			SurveyAnswerStatsBean s1 = (SurveyAnswerStatsBean) r1.getData();
//			SurveyAnswerStatsBean s2 = (SurveyAnswerStatsBean) r2.getData();
//			return s1.getSurveyDTO().getTitle()
//					.compareTo(s2.getSurveyDTO().getTitle());
//		}
//	};
//
//	private static Comparator<Row> byStartDate = new Comparator<Row>() {
//		public int compare(Row r1, Row r2) {
//
//			int returnedInt = 0;
//
//			SurveyAnswerStatsBean s1 = (SurveyAnswerStatsBean) r1.getData();
//			SurveyAnswerStatsBean s2 = (SurveyAnswerStatsBean) r2.getData();
//
//			Date date1 = s1.getSurveyDTO().getStartDate();
//			Date date2 = s2.getSurveyDTO().getStartDate();
//
//			if (date1.after(date2)) {
//				returnedInt = 1;
//			} else if (date1.before(date2)) {
//				returnedInt = -1;
//			} else if (date1.equals(date2)) {
//				returnedInt = 0;
//			}
//
//			return returnedInt;
//		}
//	};
//
//	private static Comparator<Row> byEndDate = new Comparator<Row>() {
//		public int compare(Row r1, Row r2) {
//
//			int returnedInt = 0;
//
//			SurveyAnswerStatsBean s1 = (SurveyAnswerStatsBean) r1.getData();
//			SurveyAnswerStatsBean s2 = (SurveyAnswerStatsBean) r2.getData();
//
//			Date date1 = s1.getSurveyDTO().getEndDate();
//			Date date2 = s2.getSurveyDTO().getEndDate();
//
//			if (date1.after(date2)) {
//				returnedInt = 1;
//			} else if (date1.before(date2)) {
//				returnedInt = -1;
//			} else if (date1.equals(date2)) {
//				returnedInt = 0;
//			}
//
//			return returnedInt;
//		}
//	};

	private class Row {
		private int row;

		public Row(int row) {
			this.row = row;
		}

		public Object getData() {
			int originalIndex = model.getRowIndex();
			model.setRowIndex(row);
			Object thisRowData = model.getRowData();
			model.setRowIndex(originalIndex);
			return thisRowData;
		}
	}

	public SurveyStatsQSortFilterModel() { // mandated by JSF spec
		this((SurveyStatsBean[]) null);
	}

	public SurveyStatsQSortFilterModel(SurveyStatsBean[] surveyList) { // recommended
																					// by
																					// JSF
																					// spec
		setWrappedData(surveyList);
	}

	public SurveyStatsQSortFilterModel(
			DataModel<SurveyStatsBean> model) {
		this.model = model;
		initializeRows();
	}

//	public String sortByTitle() {
//		Arrays.sort(rows, byTitle);
//		return null;
//	}
//
//	public String sortByStartDate() {
//		Arrays.sort(rows, byStartDate);
//		return null;
//	}
//
//	public String sortByEndDate() {
//		Arrays.sort(rows, byEndDate);
//		return null;
//	}

	public void setRowIndex(int rowIndex) {
		if (rowIndex == -1 || rowIndex >= model.getRowCount()) {
			model.setRowIndex(rowIndex);
		} else {
			model.setRowIndex(rows[rowIndex].row);
		}
	}

	// The following methods delegate directly to the decorated model

	public boolean isRowAvailable() {
		return model.isRowAvailable();
	}

	public int getRowCount() {
		return model.getRowCount();
	}

	public SurveyStatsBean getRowData() {
		return model.getRowData();
	}

	public int getRowIndex() {
		return model.getRowIndex();
	}

	public Object getWrappedData() {
		return model.getWrappedData();
	}

	public void setWrappedData(Object data) {
		model.setWrappedData(data);
		initializeRows();
	}

	public void addDataModelListener(DataModelListener listener) {
		model.addDataModelListener(listener);
	}

	public DataModelListener[] getDataModelListeners() {
		return model.getDataModelListeners();
	}

	public void removeDataModelListener(DataModelListener listener) {
		model.removeDataModelListener(listener);
	}

	private void initializeRows() {
		int rowCnt = model.getRowCount();
		if (rowCnt != -1) {
			rows = new Row[rowCnt];
			for (int i = 0; i < rowCnt; ++i) {
				rows[i] = new Row(i);
			}
		}
	}
}
